#ifndef _SYSTEMTIME_H_
#define _SYSTEMTIME_H_

#include <ostream>
#include <string>
#include <windows.h>

#ifndef _GNUC_
typedef __int64 longlong;
#else 
typedef long long longlong;
#endif

class SystemTime  
{
	union {
		longlong systime;
		FILETIME filetime;
	};
public:
	static SystemTime get_cur_time()
	{
		SystemTime st;
		st.set_cur_time();
		return st;
	}
	SystemTime() : systime(0) {}
	SystemTime(const SystemTime& st) : systime(st.systime) {}
	SystemTime(FILETIME ft) : filetime(ft) {}
	SystemTime(longlong ll) : systime(ll) {}
	std::string get_string() const;
	std::string get_utc_string() const;
	std::string get_db_string() const;
	operator longlong() const { return systime; }
	operator const FILETIME&() {return filetime;}
	SystemTime& set_cur_time();
	longlong get_total_seconds() const {return systime/10000000i64;}
	longlong get_total_useconds() const {return systime/10000i64;}
	SystemTime& operator=(const SystemTime& st)
	{
		systime=st.systime;
		return *this;
	}
	SystemTime& operator=(FILETIME ft)
	{
		filetime=ft;
		return *this;
	}
	SystemTime& operator=(longlong st)
	{
		systime=st;
		return *this;
	}
	virtual ~SystemTime() {}
	SystemTime& operator += (const SystemTime& st)
	{
		systime += st.systime;
		return *this;
	}
	SystemTime& operator += (unsigned milliseconds)
	{
		systime += ((longlong)milliseconds)*10000i64;
		return *this;
	}
	SystemTime& increase(long days,short hours, short minutes, short seconds)
	{
		systime +=	10000000i64*((longlong)seconds+
				60*((longlong)minutes+
				60*((longlong)hours+24*days)));
		return *this;
	}
	SystemTime& operator -= (const SystemTime& st)
	{
		systime -= st.systime;
		return *this;
	}
	SystemTime& operator -= (unsigned milliseconds)
	{
		systime -= ((longlong)milliseconds)*10000i64;
		return *this;
	}
	SystemTime& decrease(long days,short hours, short minutes, short seconds)
	{
		longlong tmp=	10000000i64*((longlong)seconds+
				60*((longlong)minutes+
				60*((longlong)hours+24*days)));

		systime -= tmp;
		return *this;
	}
	SystemTime operator+(const SystemTime& st) { return (SystemTime(*this) += st); }
	SystemTime operator+(unsigned milliseconds) { return (SystemTime(*this) += milliseconds); }
	SystemTime operator+(short days) { return (SystemTime(*this) += days); }
	SystemTime operator-(const SystemTime& st) { return (SystemTime(*this) -= st); }
	SystemTime operator-(unsigned milliseconds) { return (SystemTime(*this) -= milliseconds); }
	SystemTime operator-(short days) { return (SystemTime(*this) -= days); }
	SystemTime& operator++() { return (*this) += (unsigned)1; }
	SystemTime& operator--() { return (*this) -= (unsigned)1; }
};

std::ostream& operator<<(std::ostream& s, const SystemTime& systime);
std::ostream& put_useconds(std::ostream& s, longlong systime);

#endif